/* This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package Vista;

import Controlador.Informe.GestorInformes;
import Controlador.Informe.GestorInformesPDF;
import Controlador.*;
import java.util.*;

/**
 * Clase de debug utilizada para hacer pruebas sobre el sistema
 * @author Armando Machuca Llorente
 */
public class CommandLineApplication {

    boolean fail = false;

    public static void main(String args[]) {
        GestorPlugins GPlug = new GestorPlugins();
        CommandLineApplication ap = new CommandLineApplication();
        Vector path_plugins = ap.getPlugins(args);
        Vector devices = ap.getDevices(args);
        String output = ap.getOutputFile(args);

        if (!ap.fail) {
            //Creamos el vector de plugins
            Vector plugins = new Vector();

            Iterator i = path_plugins.iterator();
            //Cargamos los plugins de los path pasados por fichero
            while (i.hasNext()) {
                String fichero = (String) i.next();
                plugins.add(GPlug.cargarPlugin(fichero));
            //plugins.add(GPlug.cargarPlugin("C:\\Documents and Settings\\admin\\workspace\\AFA\\build\\classes\\Plugins\\Camouflagge.xml"));
            }

            Analizador a = new Analizador();
            //Analizador a=new Analizador("C:\\",plugins);
            a.setPlugins(plugins);
            a.setUnidades(devices);
            a.categorizar();
            a.ejecutar(3);
            GestorInformes g = new GestorInformes();
            g.escribirInformeXML("prueba_inf.xml", plugins);
            GestorInformesPDF gr = new GestorInformesPDF();
            gr.escribirInformePDF(output, a);
        }
    }

    public Vector getPlugins(String parametros[]) {
        Vector plugins = new Vector();
        boolean start = false;
        for (int i = 0; i < parametros.length; i++) {
            if (parametros[i].equalsIgnoreCase("-p")) {
                start = true;
            } else {
                if (parametros[i].equalsIgnoreCase("-d") || parametros[i].equalsIgnoreCase("o")) {
                    start = false;
                } else {
                    if (start) {
                        plugins.add(parametros[i]);
                    }
                }
            }
        }
        if (plugins.size() < 1) {
            usage();
        }

        return plugins;
    }

    public Vector getDevices(String parametros[]) {
        Vector devices = new Vector();
        boolean start = false;
        for (int i = 0; i < parametros.length; i++) {
            if (parametros[i].equalsIgnoreCase("-d")) {
                start = true;
            } else if (parametros[i].equalsIgnoreCase("-p") || parametros[i].equalsIgnoreCase("-o")) {
                start = false;
            } else {
                if (start) {
                    devices.add(parametros[i]);
                }
            }
        }
        if (devices.size() < 1) {
            usage();
        }

        return devices;
    }

    public String getOutputFile(String parametros[]) {
        String out = "";
        boolean start = false;
        for (int i = 0; i < parametros.length; i++) {
            if (parametros[i].equalsIgnoreCase("-o")) {
                start = true;
            } else if (parametros[i].equalsIgnoreCase("-p") || parametros[i].equalsIgnoreCase("-d")) {
                start = false;
            } else {
                if (start) {
                    out = parametros[i];
                }
            }
        }
        if (out.length() < 1) {
            usage();
        }

        return out;
    }

    public void usage() {
        if (!fail) {
            fail = true;
            System.out.println("Please , usage clap -p PluginPath1 ... PluginPathN  -d device1 ... deviceN -o outputFile.pdf");
        }
    }
}
